package com.leoman.controller.backend;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.entity.AppUserGain;
import com.leoman.entity.CompanyUserGain;
import com.leoman.service.AppUserGainService;
import com.leoman.service.CompanyUserGainService;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 财务管理
 * Created by 涂奕恒 on 2016/10/24.
 */
@Controller
@RequestMapping(value = "backend/finance")
public class FinanceController extends CommonController {

    @Autowired
    private CompanyUserGainService companyUserGainService;

    @Autowired
    private AppUserGainService appUserGainService;

    @RequestMapping("index")
    public String index() {
        return "backend/财务收入列表";
    }

    @RequestMapping("gain/index")
    public String gainIndex() {
        return "backend/提现列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     Integer companyUserGainId,
                     String mobile,
                     Integer type,
                     Integer draw,
                     Integer start,
                     Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<CompanyUserGain> page = companyUserGainService.pageByParams(companyUserGainId, mobile, type, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("/gainList")
    public void gainList(HttpServletResponse response,
                         Integer gainId,
                         String nickName,
                         String aliPayAccount,
                         Integer requestStatus,
                         Integer draw,
                         Integer start,
                         Integer length) {
        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<AppUserGain> page = appUserGainService.pageByParams(gainId, nickName, aliPayAccount, requestStatus, pageNum, length);

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    /**
     * 确认打款
     *
     * @param gainId 申请体现ID
     * @return
     */
    @RequestMapping("/confirmMoney")
    @ResponseBody
    public Integer confirmMoney(Integer gainId) {
        try {
            return appUserGainService.confirmMoney(gainId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
